/**
 * Created by hxpeng on 2017/5/11.
 */
(function () {
    'use strict';

    angular.module('App.pages.member')
        .controller('AddStudentCtrl', AddStudentCtrl);

    function AddStudentCtrl(AppUtils,$scope,$http,MemberModel,$location,Global,$state,$timeout,LoginAccountAuth) {
        var vm = this;
        vm.showScanQRCode = true;
        vm.showConfirm = false;
        vm.student = {};

        var currentPath = $location.url();
        if(currentPath.indexOf("#") != -1){
            currentPath = currentPath.split('#')[0];
        }
        currentPath = Global.path.wechat_path + currentPath;
        $http.post(Global.path.server_sdk_auth_path,{"url":currentPath})
            .success(function(data){
                wx.config({
                    debug: false,
                    appId: data.appId,
                    timestamp: data.timestamp,
                    nonceStr: data.noncestr,
                    signature: data.signature,
                    jsApiList: ['scanQRCode']
                });
                wx.error(function(res){
                    console.log(res);
                });
            })
            .error(function(data,header,config,status){
                alert("请求微信sdk校验异常");
            });

        //var result = '{"memberId":"041DFD58D85D31A478B881F1EBE4ECA9","memberName":"吴永琪","memberBirthday":"2007-05-11 16:22:33","avatarPath":"http://www.hxpeng.com/images/user/aaaaa.png","studentNumber":1}';
        ////var result = "{memberId:dwdawdawdawda}"
        //
        //if(!AppUtils.isUndefinedOrNull(result)){
        //    var obj = JSON.parse(result);
        //    if(!AppUtils.isUndefinedOrNull(obj) || !AppUtils.isUndefinedOrNull(obj.memberId)){
        //        vm.student = obj;
        //        vm.showConfirm = true;
        //    }
        //}

        //打开扫码
        $scope.scanQRCode = function(){
            wx.scanQRCode({
                needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType: ["qrCode","barCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success: function (res) {
                    //获得二维码的json数据
                    var result = res.resultStr;
                    //var result = '{"memberId":"041DFD58D85D31A478B881F1EBE4ECA9","memberName":"吴永琪","memberBirthday":"2007-05-11 16:22:33","avatarPath":"http://www.hxpeng.com/images/user/aaaaa.png","studentNumber":1}';

                    if(!AppUtils.isUndefinedOrNull(result)){
                        var obj = JSON.parse(result);
                        if(!AppUtils.isUndefinedOrNull(obj) || !AppUtils.isUndefinedOrNull(obj.memberId)){
                            vm.showScanQRCode = false;
                            $timeout(function(){
                                vm.student = obj;
                            },0)
                            vm.showConfirm = true;
                        }else{
                            alert(1);
                        }
                    }else{
                        AppUtils.showAlert("请扫描正确的二维码！");
                    }
                }
            });
        }


        //提交绑定
        $scope.submitInfo = function(){
            if(!AppUtils.isUndefinedOrNull(vm.student.memberId)){
                MemberModel.studentBindParent({memberId : vm.student.memberId}).$promise.then(function(response) {
                    if(!AppUtils.isUndefinedOrNull(response) && response.stat == 'success'){
                        $location.path("/");
                        AppUtils.showAlert("添加成功！");
                        LoginAccountAuth.setAccount(response.loginAccountVo);
                    }else if(response.stat == 'err'){
                        AppUtils.showAlert(response.msg);
                    }else{
                        AppUtils.showAlert("提交失败，请稍后再试！");
                    }
                });
            }else{
                AppUtils.showAlert("提交失败，刷新页面试试！");
            }

        }


    }
})();